<!-- 角色编辑弹窗 -->
<template>
  <ele-modal width="460px"
             :visible="visible"
             :close-on-click-modal="true"
             :title="isUpdate ? '修改角色' : '添加角色'"
             @update:visible="updateVisible">
    <el-form ref="form"
             :model="form"
             :rules="rules"
             label-width="82px">
      <el-form-item label="角色名称:"
                    prop="name">
        <el-input clearable
                  :maxlength="20"
                  v-model="form.name"
                  placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色类型:"
                    prop="type">

        <!-- 选择类型 -->
        <el-select clearable
                   v-model="form.type"
                   class="ele-block"
                   :placeholder="placeholder">
          <el-option v-for="item in dataList"
                     :key="item.id"
                     :value="item.id"
                     :label="item.name" />
        </el-select>

      </el-form-item>

    </el-form>

    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary"
                 @click="save"
                 :loading="loading">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>
  
  <script>
import { addRole, updateRole } from '@/api/system/role';


export default {
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data () {
    const defaultForm = {
      id: null,
      name: '',
      type: '',

    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm },
      dataList: [
        {
          id: 1,
          name: '后台管理员'
        },
        {
          id: 2,
          name: '门店工作人员'
        },
        {
          id: 3,
          name: '门店管理者'
        }
      ],
      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请输入角色类型',
            trigger: 'blur'
          }
        ],

      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  methods: {
    /* 保存编辑 */
    save () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        const saveOrUpdate = this.isUpdate ? updateRole : addRole;
        saveOrUpdate(this.form)
          .then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.updateVisible(false);
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      });
    },
    /* 更新visible */
    updateVisible (value) {
      this.$emit('update:visible', value);
    }
  },
  watch: {
    visible (visible) {
      if (visible) {
        if (this.data) {
          this.$util.assignObject(this.form, this.data);
          this.isUpdate = true;
        } else {
          this.isUpdate = false;
        }
      } else {
        this.$refs.form.clearValidate();
        this.form = { ...this.defaultForm };
      }
    }
  }
};
  </script>
  